
uniform sampler2D fbo_texture;
uniform sampler2D depth_texture;
in vec2 TexCoords;
out vec4 FragmentColor;
uniform float FocalDistance;
 
float gauss(float ordo, int i) 
{
	return exp(-0.5 * pow(float(i)/ordo,2));
}

vec3 blur(vec2 TexCoordsIn, int s)
{
	float ordo = 5.0;
	float sum = 0.0;
	vec3 res = vec3(0.0, 0.0, 0.0);
	vec4 TexFetch = vec4(0.0, 0.0, 0.0, 0.0);
	float pixelSize = 1.0 / 1024.0;
	vec4 thresh1 = vec4(0.1, 0.3, 0.5, -0.01);
	vec4 thresh2 = vec4(0.6, 0.7, 0.8, 0.9);
	float thresh[7];
	//thresh[0] = 0.9;
	thresh[0] = 0.8;
	thresh[1] = 0.7;
	thresh[2] = 0.6;
	thresh[3] = 0.5;
	thresh[4] = 0.3;
	thresh[5] = 0.1;
	thresh[6] = -0.01;
	
	
	//vec4 TexFetchCenter = texure2D(
	for(int i = -s + 1; i < s; i ++)
	{
		float temp = 1.0;//gauss(ordo, i);
		float blend = TexFetch.a / 0.3;
		TexFetch = texture2D(fbo_texture, TexCoordsIn + vec2(pixelSize * float(i), 0.0));
		//if((abs(TexFetch.a) < 0.1 && TexFetch.a > 1.0) || i == 0)
		//if(TexFetch.a > 1.0)
		//{
		int index = 0;
		if(i < 0)
			index = i + 6;
		else
			index = 6 - i;
		float tempWeight = clamp(TexFetch.a  - thresh[index], 0.0, 1.0);
		res += TexFetch.xyz * temp * tempWeight;
		sum += tempWeight;
		//}
	}
	return res / sum;
} 

void main(void) {
	vec2 NewTexCoords = ((TexCoords) + 1.0) * 0.5;
	vec4 DepthSample = texture2D(fbo_texture, NewTexCoords);
  //vec4 DepthSample = texture2D(fbo_texture, NewTexCoords);
   // float fl = clamp(400.0 * abs(DepthSample.a - 0.4), 0.0, 10.0);
//	int size = int(fl);
  vec3 TextureSample;
  int size = 5;
  TextureSample = blur(NewTexCoords, size);//texture2D(fbo_texture, NewTexCoords);
  
  FragmentColor = vec4(TextureSample, DepthSample.a);	

}

